using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;

namespace Sipek
{
    public partial class frmCallLog : Form
    {
        public frmCallLog()
        {
            InitializeComponent();
        }

        private void CRUD(String sSQLCommand)
        {
            System.Data.Odbc.OdbcConnection conn = new System.Data.Odbc.OdbcConnection();
            //conn.ConnectionString = "DSN=kubikcrm";
            conn.ConnectionString = "DSN=kubikcrm";
            // SQL command
            //sSQLCommand = "INSERT INTO activity_transaction(activity_id)VALUES(55);";
            OdbcCommand cmdAdder = new OdbcCommand(sSQLCommand, conn);
            cmdAdder.Connection.Open(); // Create the command object
            cmdAdder.ExecuteNonQuery(); // Execute the SQL command
        }

        private void frmCallLog_Load(object sender, EventArgs e)
        {
            txtCustomerName.Text=Properties.Settings.Default.CustomerName;
            txtPic.Text = Properties.Settings.Default.CustomerPic;
            txtTlp.Text = Properties.Settings.Default.CustomerTlp;
            txtContact.Text = "";
            txtDesc.Text = "";
            
            //MessageBox.Show("test");
        }

        void ClearCustomer()
        {
            Properties.Settings.Default.CustomerID = "";
            Properties.Settings.Default.CustomerName = "";
            Properties.Settings.Default.CustomerPic = "";
            Properties.Settings.Default.CustomerStartDate = "";
            Properties.Settings.Default.CustomerStartTime = "";
            Properties.Settings.Default.CustomerTlp = "";
        }

        private void tbSave_Click(object sender, EventArgs e)
        {
            Byte id;
            if(cbType.Text=="Call")
                id=1;
            else if(cbType.Text=="Invalid Data")
            {
                id=2;
            }
            else if (cbType.Text == "Meeting")
            {
                id = 4;
            }
            else
            {
                id = 6;
            }

            if (id != 0)
            {
                //Properties.Settings.Default.CustomerStartTime = Properties.Settings.Default.CustomerStartTime;
                //MessageBox.Show(Properties.Settings.Default.CustomerStartTime);
                String sql,endTime;
                DateTime DD = DateTime.Now;
                endTime = String.Format("{0:HH:mm:ss}",DD);
                sql = "INSERT INTO activity_transaction(activity_date,activity_time,activity_time2,activity_id,pic,customer_id,activity_contact,activity_desc,user_stamp,date_stamp) VALUES(CURDATE(),'" + Properties.Settings.Default.CustomerStartTime + "','" + endTime + "'," + id + ",'SRL'," + Properties.Settings.Default.CustomerID + ",'" + txtContact.Text + "','" + txtDesc.Text + "','SRL',CURDATE())";
                CRUD(sql);
                sql = "UPDATE customers SET latest_activity=" + id + " WHERE id= " + Properties.Settings.Default.CustomerID;
                CRUD(sql);
                ClearCustomer();
             
                MessageBox.Show("Data Activity dan Latest Activity Customer Sudah Tersimpan ! ");
                this.Close();
                

            }
            else
            {
                MessageBox.Show("Please Fill Type Of Call");
                cbType.Focus();
            }
        }
    }
}